package com.sixbro.shiro.common.utils;

import com.sixbro.shiro.common.exception.code.ApiResponseCode;
import com.sixbro.shiro.common.exception.code.ResponseCodeInterface;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * <p>
 *
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/19 13:47
 */
@Data
public class ApiResult<T> {
    /**
     * 请求响应code，0为成功 其他为失败
     */
    @ApiModelProperty(value = "请求响应code，0为成功 其他为失败", name = "code")
    private int code;

    /**
     * 响应异常码详细信息
     */
    @ApiModelProperty(value = "响应异常码详细信息", name = "message")
    private String message;

    /**
     * 响应内容 ， code 0 时为 返回 数据
     */
    @ApiModelProperty(value = "需要返回的数据", name = "data")
    private T data;

    public ApiResult(int code, T data) {
        this.code = code;
        this.data = data;
        this.message = null;
    }

    public ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResult(int code, String message) {
        this.code = code;
        this.message = message;
        this.data=null;
    }

    public ApiResult() {
        this.code = ApiResponseCode.SUCCESS.getCode();
        this.message = ApiResponseCode.SUCCESS.getMessage();
        this.data = null;
    }

    public ApiResult(T data) {
        this.data = data;
        this.code = ApiResponseCode.SUCCESS.getCode();
        this.message = ApiResponseCode.SUCCESS.getMessage();
    }

    public ApiResult(ResponseCodeInterface responseCodeInterface) {
        this.data = null;
        this.code = responseCodeInterface.getCode();
        this.message = responseCodeInterface.getMessage();
    }

    public ApiResult(ResponseCodeInterface responseCodeInterface, T data) {
        this.data = data;
        this.code = responseCodeInterface.getCode();
        this.message = responseCodeInterface.getMessage();
    }

    /**
     * 操作成功 data为null
     * @throws
     */
    public static <T>ApiResult success(){
        return new ApiResult();
    }
    /**
     * 操作成功 data 不为null
     * @throws
     */
    public static <T>ApiResult success(T data){
        return new ApiResult(data);
    }

    /**
     * 操作失败 data 不为null
     * @throws
     */
    public static <T>ApiResult fail(String msg){
        return new ApiResult(ApiResponseCode.OPERATION_ERRO.getCode(),msg);
    }

    /**
     * 自定义 返回操作 data 可控
     * @throws
     */
    public static <T>ApiResult getResult(int code,String msg,T data){
        return new ApiResult(code,msg,data);
    }
    /**
     *  自定义返回  data为null
     * @throws
     */
    public static <T>ApiResult getResult(int code,String msg){
        return new ApiResult(code,msg);
    }
    /**
     * 自定义返回 入参一般是异常code枚举 data为空
     * @throws
     */
    public static <T>ApiResult getResult(ApiResponseCode apiResponseCode){
        return new ApiResult(apiResponseCode);
    }
    /**
     * 自定义返回 入参一般是异常code枚举 data 可控
     * @throws
     */
    public static <T>ApiResult getResult(ApiResponseCode apiResponseCode,T data){
        return new ApiResult(apiResponseCode,data);
    }
}
